<?php
namespace app\api\controller;

use app\common\controller\ApiBase;
use app\common\model\KfUser as KfUserModel;
use app\common\model\KfFaq as KfFaqModel;


class KfFaq extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
    }


    public function index()
    {
        if (!isset($this->param['cpm']) ||  empty($this->param['cpm'])){
            $this->ajaxReturn(false, 'cpm error');
        }
        $group=\Wechat::$customer_group;
        $group=array_combine(array_column($group,'str'),$group);
        $kf_faq=KfFaqModel::where('cpm',$this->param['cpm'])->where('status',1)->select()->each(function ($val) use($group){
            $group_name=[];
            if(!empty($val['group'])){
                $str=explode(',', $val['group']);
                foreach ($str as $key => $value) {
                    if(array_key_exists($value,$group)) $group_name[]=$group[$value]['content'];
                }
            }
            $val['group_name']=implode(',',$group_name);
            $val['createtime']=date('Y-m-d H:i:s',$val['time']);
        });
        $result=[];
        if(!$kf_faq->isEmpty()){
            $result=$kf_faq->toArray();
        }
        $this->ajaxReturn($result);
    }
    public function info()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        $kf_faq=KfFaqModel::where('id',$this->param['id'])->find();
        if(empty($kf_faq)){
            $this->ajaxReturn(false,'不存在该客服');
        }
        $this->ajaxReturn($kf_faq);
    }

    public function add()
    {
        if (!isset($this->param['cpm']) ||  empty($this->param['cpm'])){
            $this->ajaxReturn(false, 'cpm error');
        }
        if (!isset($this->param['group']) ||  empty($this->param['group'])){
            $this->ajaxReturn(false, '请选择分组');
        }
        if (!isset($this->param['question']) ||  empty($this->param['question'])){
            $this->ajaxReturn(false, '请填写名称');
        }
        if(!isset($this->param['answer']) ||  empty($this->param['answer'])){
            $this->ajaxReturn(false, '请选择绑定的用户');
        }

        $kf_faq= KfFaqModel::where('cpm',$this->param['cpm'])->find();
        if(!empty($kf_faq)){
            $this->ajaxReturn(false,'已经存在');
        }
        $group=implode(',',$this->param['group']);
        $data=['cpm'=>$this->param['cpm'],'group'=>$group,'question'=>$this->param['question'],'answer'=>$this->param['answer'],'time'=>time()];
        if(isset($this->param['sort']) && !empty($this->param['sort'])){
            $data['sort']=(int)$this->param['sort'];
        }
        KfFaqModel::create($data);
        $this->ajaxReturn('ok','添加成功');
    }
    public function edit()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        if (!isset($this->param['group']) ||  empty($this->param['group'])){
            $this->ajaxReturn(false, '请选择分组');
        }
        if (!isset($this->param['question']) ||  empty($this->param['question'])){
            $this->ajaxReturn(false, '请填写名称');
        }
        if(!isset($this->param['answer']) ||  empty($this->param['answer'])){
            $this->ajaxReturn(false, '请选择绑定的用户');
        }

        $kf_faq=KfFaqModel::where('id',$this->param['id'])->find();
        if(empty($kf_faq)){
            $this->ajaxReturn(false,'问题不存在');
        }
        $group=implode(',',$this->param['group']);
        $data=['group'=>$group,'question'=>$this->param['question'],'answer'=>$this->param['answer'],'time'=>time()];
        if(isset($this->param['sort']) && !empty($this->param['sort'])){
            $data['sort']=(int)$this->param['sort'];
        }
        KfFaqModel::update($data, ['id' => $this->param['id']]);
        $this->ajaxReturn('ok','修改成功');
    }
    public function delete()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        $kf_faq=KfFaqModel::where('id',$this->param['id'])->where('status',1)->find();
        if(!empty($kf_faq)){
            KfFaqModel::where('id',$this->param['id'])->update(['status'=>0,'time'=>time()]);
        }
        $this->ajaxReturn('ok','删除成功');
    }


}
